<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title></title>
    <script src="../js/mui.js"></script>
    <link href="../css/mui.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="../css/calendar.css" />
</head>
<body>
	<!--<div id="date"></div>-->
	<div class="mui-content">
		<div class="calen-top">
			<div class="mui-clearfix">
				<div class="mui-pull-right clearDateStatus">清除</div>
		   		<div class="close-x mui-action-back mui-pull-left"><img src="../icon/calen-close.png" alt="" /></div>
			</div>
		   	<div class="showDate mui-clearfix">
		   		<div class="mui-pull-left">
		   			<div id="startDate">租赁</div>
		   			<div id="startDay">&nbsp;</div>
		   		</div>
		   		<div class="mui-pull-left">
		   			<div id="endDate">归还</div>
		   			<div id="endDay">&nbsp;</div>
		   		</div>
		   	</div>
		   	<div class="week">
		   		<ul class="date_week_ul">
		   			<li>日</li>
		   			<li>一</li>
		   			<li>二</li>
		   			<li>三</li>
		   			<li>四</li>
		   			<li>五</li>
		   			<li>六</li>
		   		</ul>
		   	</div>
	   	</div>
	   	<div class="datediv">
	   		<div class="mui-scroll-wrapper" id="refreshContainer">
	   		    <div class="mui-scroll">
	   		        <div id="date"></div>
	   		    </div>
	   		</div>
	   	</div>
	   	<div class="cal-btn">
	   		<div class="mui-clearfix enter2" style="position: fixed; bottom: 0px;left: 0px;width: 100%;height: 60px;">
	   			<div class="">
	   				<span class="mui-pull-left" style="line-height: 59px;padding-left: 10px;color: #666;font-size:12px; color: #000;">请确认数量</span>
		   			<div class="mui-numbox " style="margin-top: 13px;margin-right: 10px;" data-numbox-min="1">
						<button class="mui-btn mui-numbox-btn-minus" type="button">-</button>
						<input class="mui-numbox-input" type="number" />
						<button class="mui-btn mui-numbox-btn-plus" type="button">+</button>
					</div>
					<button class="mui-btn bexta-btn-red mui-pull-right gogo" style="margin-right: 10px;margin-top: 10px;padding:  10px 16px">确定</button>
				</div>
	   		</div>
	   		<div class="mui-content-padded enter1 " >
	   			<button type="button" class="mui-btn mui-btn-block mui-btn-rander">确定</button>
	   		</div>
	   	</div>
	</div>
	
<script src="../js/bexta.js"></script>
<script src="../js/calendar.js"></script>
<script type="text/javascript">
	var odata = null, 
	inDate = [];  // 存储选择好的日历
	mui.init(
		{
		pullRefresh:{
			container:"#refreshContainer",
			up:{
				height:50,
		      	auto:false,
		      	contentrefresh : " ",
		      	contentnomore:' ',
		      	callback :addDate
		    }
		}
	}
		);
	 mui.ready(function () {
	 	document.querySelector('.calen-top').style.paddingTop = bexta.getStatusHeight() + 'px';  	
	 })
	function joinConfirm(obj){
//		bexta.ajax(api.add_cart, function(res,msg){
//					if( !res ){
						bexta.setStorage('cartStatus', 1);
						bexta.ajax(api.buy_now, function(res){
							if( res.order_no ){
								bexta.openWithTitle({
									id:'confirmOrder',
									url:'confirmOrder.html',
									extras:{
										order_no:res.order_no
									}
								},{
									title:{
										text:'订单确认'
									}
								})
							}
						},{data:obj})
//					}
//				}, {data:obj});
	}
	mui.plusReady(function () {
		var view = bexta.getWebview();
		var type = view.type;
		document.querySelector('.mui-btn-rander').addEventListener('tap', function() {
			if( type == 'detail' && inDate.length != 0 ){
				var formatDate = bexta.getDate(inDate);
				if( formatDate.length == 2 ){
					var start_time =  formatDate[0].allDate;
					var end_time = formatDate[1].allDate;
				}else{
					var start_time =  formatDate[0].allDate;
					var end_time = formatDate[0].allDate;
				}
				if( view.commend_id == 1 || view.commend_id==6 || view.commend_id==7 || view.commend_id== 8){
					document.querySelector('.enter1').style.display = 'none';
					document.querySelector('.enter2').style.display = 'block';
					return ;
				}else{
					joinConfirm({
							uid:view.uid,
							goods_id:view.goods_id,
							product_id:view.product_id,
							start_time:start_time,
							end_time:end_time
					});
					
				}
				
				
				
				
				
			}else if(type=='enquiry' || type == 'search' && inDate.length != 0 ){
				//返回搜索
				if( inDate.length < 1 ){
					mui.toast("至少要选择一个日期");
					return ;
				}else{
					mui.fire(bexta.parentsView(), 'toggleDate', {
						date:inDate
					});
					mui.back();
				}
				
			}else{
				mui.toast("至少要选择一个日期");
			}
		});
		
		
		document.querySelector('.gogo').addEventListener('tap', function(){
			var nums  =mui('.mui-numbox').numbox().getValue();
			var formatDate = bexta.getDate(inDate);
				if( formatDate.length == 2 ){
					var start_time =  formatDate[0].allDate;
					var end_time = formatDate[1].allDate;
				}else{
					var start_time =  formatDate[0].allDate;
					var end_time = formatDate[0].allDate;
				}
				joinConfirm({
							uid:view.uid,
							goods_id:view.goods_id,
							product_id:view.product_id,
							start_time:start_time,
							end_time:end_time,
							num:nums
					});
			
			
		});
		
		if( type == 'search' || type == 'enquiry' ){
			showDate();
		}else if(type == 'detail'){
			//获取档期
			showDate(view.timer);
		}
		
		
		
	});
	function showDate(deadlines){
		var datediv = document.querySelector('.datediv'),
			calentop = document.querySelector('.calen-top');
		var wh = window.innerHeight,
			calentopHeight= calentop.offsetHeight;
		datediv.style.height = (wh-60)-calentopHeight + 'px';
		
		mui('.mui-scroll-wrapper').scroll({});
		var startDate = document.querySelector('#startDate'),
			startDay = document.querySelector('#startDay'),
			endDate = document.querySelector('#endDate'),
			endDay = document.querySelector('#endDay');
	   	mui().fullDate({
	   		id:'date',
	   		index:3,
	   		clearBtn:'clearDateStatus',  //清除按钮 class name
	   		deadline:deadlines || [],     //档期， 时间截数组  毫秒 
	   		success:function(data, obj){
	   			if( data.timer.length  == 0 ){
	   				inDate = [];
	   			}else{
	   				var eb = bexta.getDate(data.timer);
		   			startDate.innerHTML = eb[0].date;
		   			startDay.innerHTML = eb[0].day;
		   			inDate = data.timer;
		   			if( eb.length > 1 ){
		   				endDate.innerHTML = eb[1].date;
		   				endDay.innerHTML = eb[1].day;
		   			}else{
		   				endDate.innerHTML = eb[0].date;
		   				endDay.innerHTML = eb[0].day;
		   			}
	   			}
	   		},
	   		initCall:function(no){
	   			odata = no;
	   		}
	   	});
	}
	function addDate(){
		var that = this;
		odata.addDate(function(){
			mui.later(function(){
				that.endPullupToRefresh(false);
			}, 1500);
			
		});
	}
</script>
</body>
</html>